package org.platform.lwc.netty.config;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
@Slf4j
public class MessagePushConfig implements InitializingBean {
    @Resource
    private EventListenner eventListenner;
    @Autowired
    private SocketProperties properties;


    @Autowired
    private SocketIOServer socketIOServer;

    @Override
    public void afterPropertiesSet() throws Exception {
        socketIOServer.start();
        System.out.println("启动正常");
    }

    @Bean
    public SocketIOServer socketIOServer() {

        Configuration config = new Configuration();
        config.setPort(properties.getPort());
        config.setWorkerThreads(properties.getWorkCount());
        config.setAllowCustomRequests(properties.getAllowCustomRequests());
        config.setUpgradeTimeout(properties.getUpgradeTimeout());
        config.setPingTimeout(properties.getPingTimeout());
        config.setPingInterval(properties.getPingInterval());
        config.setMaxHttpContentLength(properties.getMaxHttpContentLength());
        config.setMaxFramePayloadLength(properties.getMaxFramePayloadLength());

        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setReuseAddress(true);
        socketConfig.setTcpNoDelay(true);
        socketConfig.setSoLinger(0);
        config.setSocketConfig(socketConfig);
//        config.setKeyStorePassword("证书密码");
//        InputStream stream = this.getClass().getResourceAsStream("Resource目录下的证书");
//        config.setKeyStore(stream);

        SocketIOServer server = new SocketIOServer(config);
        server.addListeners(eventListenner);

        return server;
    }
}
